package com.qidian.mes.cal.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidian.mes.cal.mapper.CalShiftMapper;
import com.qidian.mes.cal.service.CalShiftService;
import com.qidian.mes.pojo.cal.CalPlan;
import com.qidian.mes.pojo.cal.CalShift;
import com.qidian.mes.util.pojo.MESResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author 04冯虹丹
 * @Company 起点编程
 * @Time 2023/11/1 20:46
 * @Description
 */
@Service
@Transactional
public class CalShiftServiceImpl implements CalShiftService {

    @Autowired
    private CalShiftMapper calShiftMapper;

    /**
     * 分页查询
     * @param calShift
     * @return
     */
    @Override
    public MESResult list(int page, int limit, CalShift calShift) {
        PageHelper.startPage(page,limit);
        List<CalShift> list = calShiftMapper.list(calShift);
        PageInfo<CalShift> pageInfo = new PageInfo<>(list);
        return MESResult.ok(pageInfo);
    }

    /**
     * 添加班次
     * @param calShift
     * @return
     */
    @Override
    public MESResult save(CalShift calShift) {
        String shiftType = calShiftMapper.selectShiftType(calShift.getPlanId());
        System.out.println("========shiftType="+shiftType);
        int num = calShiftMapper.countCalShift(calShift.getPlanId());
        if(shiftType.equals("白班")){
            if(num >= 1){
                return MESResult.duplicate("轮班方式为 白班 时只能有一个班次!");
            }else {
                return MESResult.updateIsOk(calShiftMapper.save(calShift));
            }
        } else if (shiftType.equals("两班倒")) {
            if(num >= 2){
                return MESResult.duplicate("轮班方式为 两班倒 时只能有两个班次!");
            }else {
                return MESResult.updateIsOk(calShiftMapper.save(calShift));
            }
        } else if (shiftType.equals("三班倒")) {
            if(num >= 3){
                return MESResult.duplicate("轮班方式为 三班倒 时只能有三个班次!");
            }else {
                return MESResult.updateIsOk(calShiftMapper.save(calShift));
            }
        }
        return null;
    }

    /**
     * 单个删除班次
     * @param shiftId
     * @return
     */
    @Override
    public MESResult delete(Long shiftId) {
        return MESResult.updateIsOk(calShiftMapper.delete(shiftId));
    }

    /**
     * 批量删除班次
     * @param shiftId
     * @return
     */
    @Override
    public MESResult deletes(Long[] shiftId) {
        int count = calShiftMapper.deletes(shiftId);
        return MESResult.ok(count + "");
    }

    /**
     * 编辑班次
     * @param calShift
     * @return
     */
    @Override
    public MESResult update(CalShift calShift) {
        return MESResult.updateIsOk(calShiftMapper.update(calShift));
    }

    /**
     * 根据班次ID查询
     * @param shiftId
     * @return
     */
    @Override
    public MESResult get(Long shiftId) {
        CalShift calShift  = null;
        if((calShift = calShiftMapper.get(shiftId)) != null){
            return MESResult.ok(calShift);
        }
        return MESResult.empty();
    }
}
